package cn.jy.nvr.service.impl;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.jy.nvr.domain.CommunRecorder;
import cn.jy.nvr.domain.ProviderBaseInfo;
import cn.jy.nvr.domain.XY_BASE_COMMUNRECORD;
import cn.jy.nvr.mapper.ProviderBaseInfoMapper;
import cn.jy.nvr.mapper.XY_BASE_COMMUNRECORDMapper;
import cn.jy.nvr.service.ICommunRecorderService;
import cn.jy.nvr.utils.DateTools;
import cn.jy.nvr.utils.ImageUtils;
import cn.jy.nvr.utils.TimeUtils;
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
@Service
public class CommumRecorderServiceImpl implements ICommunRecorderService {
     @Autowired
	  XY_BASE_COMMUNRECORDMapper   xy_base_communrecordmapper;
	  @Autowired
	  ProviderBaseInfoMapper providerBaseInfoMapper;
	@Override
	public void add(CommunRecorder communRecorder) {
		
	}

	@Override
	public void del(Serializable ID) {
		
	}
	
    @Transactional
	@Override
	public void update(CommunRecorder communRecorder) {
    	XY_BASE_COMMUNRECORD xy_base_communrecord = xy_base_communrecordmapper.loadById(communRecorder.getID());
    	if (xy_base_communrecord==null) {
			return;
		}
    	xy_base_communrecord.setGTFS(communRecorder.getGtfs());
    	xy_base_communrecord.setGTZT("1");
    	String countDate = TimeUtils.getCountDate(communRecorder.getCreateTime() ,DateTools.getInstance().dateFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));
    	xy_base_communrecord.setWCYS(countDate);
    
    	long time = (new Date().getTime()- DateTools.getInstance().stringFormat(communRecorder.getCreateTime(), "yyyy-MM-dd HH:mm:ss").getTime())/1000;
    	xy_base_communrecord.setWCYSMS( String.valueOf(time) );
    	xy_base_communrecord.setWCSJ(DateTools.getInstance().dateFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));
    	xy_base_communrecord.setRIL1(communRecorder.getRIL1());
    	xy_base_communrecord.setRIL2(communRecorder.getRIL2());
    	xy_base_communrecord.setRIL3(communRecorder.getRIL3());
    	xy_base_communrecord.setRIL4(communRecorder.getRIL4());
    	xy_base_communrecord.setRIL5(communRecorder.getRIL5());
    	xy_base_communrecord.setRIL6(communRecorder.getRIL6());
    	xy_base_communrecord.setRIL7(communRecorder.getRIL7());
    	xy_base_communrecord.setRIL8(communRecorder.getRIL8());
    	xy_base_communrecord.setRIL9(communRecorder.getRIL9());
    	xy_base_communrecord.setRIL10(communRecorder.getRIL10());
    	xy_base_communrecord.setRIL11(communRecorder.getRIL11());
    	xy_base_communrecord.setGTNR(communRecorder.getGtnr());
    	xy_base_communrecordmapper.update(xy_base_communrecord);
	}

	@Override
	public CommunRecorder getByNurseAndProvider(CommunRecorder communRecorder) {
		CommunRecorder newCommunRecorder=new CommunRecorder();
		String id = communRecorder.getID();
		XY_BASE_COMMUNRECORD xy_BASE_COMMUNRECORD2 = xy_base_communrecordmapper.loadById(id);
		if (xy_BASE_COMMUNRECORD2!=null) {
			ProviderBaseInfo providerBaseInfo = providerBaseInfoMapper.getProviderBaseInfoByID(xy_BASE_COMMUNRECORD2.getJYXM());
			if (providerBaseInfo!=null) {
				newCommunRecorder.setAge(TimeUtils.getAgeFromBirthTime(providerBaseInfo.getBIRTHDAY().substring(0, 10)));
				newCommunRecorder.setCreateTime(xy_BASE_COMMUNRECORD2.getSCSJ());
				newCommunRecorder.setGtfs(xy_BASE_COMMUNRECORD2.getGTFS());
				try {
					newCommunRecorder.setIcon(ImageUtils.byte2Base64StringFun(ImageUtils.getByte(providerBaseInfo.getICON()))  );
				} catch (IOException e) {
					e.printStackTrace();
				}
				newCommunRecorder.setID(xy_BASE_COMMUNRECORD2.getCJJLID());
				newCommunRecorder.setName(providerBaseInfo.getXM());
				newCommunRecorder.setNurse(xy_BASE_COMMUNRECORD2.getHSXM());
				newCommunRecorder.setPhone(providerBaseInfo.getPHONE());
				newCommunRecorder.setProviderNo(providerBaseInfo.getCARDID());
				if ("1".equals(providerBaseInfo.getXMID())) {
					newCommunRecorder.setSex("男");
				}
				if ("2".equals(providerBaseInfo.getXMID())) {
					newCommunRecorder.setSex("女");
				}
				if (xy_BASE_COMMUNRECORD2.getCOMMUNICATIONTYPE()!=null) {
					newCommunRecorder.setCommunType(xy_BASE_COMMUNRECORD2.getCOMMUNICATIONTYPE());
				}
			}
		}
		return newCommunRecorder;
	}

	@Override
	public List<CommunRecorder> getAllByNurse(CommunRecorder communRecorder) {
		//根据护士去拿到这个护士所有的回访记录
		List<CommunRecorder>  communRecorderList =new ArrayList<CommunRecorder>();
		XY_BASE_COMMUNRECORD xy_base_communrecord=new XY_BASE_COMMUNRECORD();
		xy_base_communrecord.setHSXM(communRecorder.getNurse());
		List<XY_BASE_COMMUNRECORD> xy_communRecorderList= xy_base_communrecordmapper.loadByNuser(xy_base_communrecord);
		if (xy_communRecorderList.size()>0) {
			for (XY_BASE_COMMUNRECORD xy_BASE_COMMUNRECORD2 : xy_communRecorderList) {
				if (xy_BASE_COMMUNRECORD2.getJYXM()==null||"".equals(xy_BASE_COMMUNRECORD2.getJYXM())) {
					break;
				}
				ProviderBaseInfo providerBaseInfo = providerBaseInfoMapper.getProviderBaseInfoByID(xy_BASE_COMMUNRECORD2.getJYXM());
				if (providerBaseInfo==null) {
					break;
				}
				CommunRecorder newCommunRecorder=new CommunRecorder();
				newCommunRecorder.setAge(TimeUtils.getAgeFromBirthTime(providerBaseInfo.getBIRTHDAY().substring(0, 10)));
				newCommunRecorder.setCreateTime(xy_BASE_COMMUNRECORD2.getSCSJ());
				newCommunRecorder.setGtfs(xy_BASE_COMMUNRECORD2.getGTFS());
				try {
					newCommunRecorder.setIcon(ImageUtils.byte2Base64StringFun(ImageUtils.getByte(providerBaseInfo.getICON()))  );
				} catch (IOException e) {
					e.printStackTrace();
				}
				newCommunRecorder.setID(xy_BASE_COMMUNRECORD2.getCJJLID());
				newCommunRecorder.setName(providerBaseInfo.getXM());
				newCommunRecorder.setNurse(xy_BASE_COMMUNRECORD2.getHSXM());
				newCommunRecorder.setPhone(providerBaseInfo.getPHONE());
				newCommunRecorder.setProviderNo(providerBaseInfo.getCARDID());
				if ("1".equals(providerBaseInfo.getXMID())) {
					newCommunRecorder.setSex("男");
				}
				if ("2".equals(providerBaseInfo.getXMID())) {
					newCommunRecorder.setSex("女");
				}
				if (xy_BASE_COMMUNRECORD2.getCOMMUNICATIONTYPE()!=null) {
					newCommunRecorder.setCommunType(xy_BASE_COMMUNRECORD2.getCOMMUNICATIONTYPE());
				}
				communRecorderList.add(newCommunRecorder);
			}
		}
		return communRecorderList;
	}

   
}
